package util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONArray;

public class HttpRequest {
	//jdk自带的HttpURLConnection
	public static InputStream httpUrlConn(String path, String method, int timeout) {
		InputStream is = null;
		OutputStream os = null;
		HttpURLConnection conn = null;
		URL url = null;
		User user = null;
		String content = "";
		if("POST".equals(method)) {
			user = new User();
			user.setAccountNumber("Anan");
			user.setPassowrd("123456");
		}else {
			content = "中国救援队呼叫马航MH370,收到请回答，over";
			try {
				path = path + "?content=" + URLEncoder.encode(content, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			url = new URL(path);
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(timeout);
			conn.setRequestMethod(method);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			if("POST".equals(method)) {
				os = conn.getOutputStream();
				os.write(JSONArray.toJSONBytes(user));
			}
			conn.connect();
			if(conn.getResponseCode() == 200) {
				is = conn.getInputStream();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return is;
	}
	
	//HttpClient
	public static void httpClient(String path, String method, int timeout) {
		//已被弃用 httpClient = new DefaultHttpClient();
		//获取DefaultHttpClient请求
//		String res = "";
//		HttpClient httpClient = HttpClientBuilder.create().build();
//		HttpPost post = new HttpPost(path);
//		//构建超时等配置信息
//        RequestConfig config = RequestConfig.custom()
//        		.setConnectTimeout(timeout) //连接超时时间
//                .build();
//        //设置请求配置时间
//        post.setConfig(config);
//		post.setHeader("accountNumber", "Anan");
//		post.setHeader("password", "123456");
//		HttpResponse response = null;
//		try {
//			response = httpClient.execute(post);
//			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//				res = EntityUtils.toString(response.getEntity());
//			} else {
//				res = "未连接到服务器....";
//			}
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println(res);
	} 
}

class User {
	private String accountNumber;
	private String passowrd;
	public String getAccountNumber() {
		return accountNumber;
	}
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}
	public String getPassowrd() {
		return passowrd;
	}
	public void setPassowrd(String passowrd) {
		this.passowrd = passowrd;
	}
	@Override
	public String toString() {
		return "User [accountNumber=" + accountNumber + ", passowrd=" + passowrd + "]";
	}
}
